﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using AutoMapper;
using DotNetCore.CAP;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace L.M.System.Application.Services
{
    public class Organization_chartServices : BaseServices<organization_chart, string>, IOrganization_chartServices
    {
        /// <summary>
        /// 组织架构
        /// </summary>
        private readonly IOrganization_chartRepository organization_ChartRepository;
        private readonly ISystem_number_tableServices system_Number_TableServices;
        private readonly ICapPublisher capPublisher;
        private readonly IMapper mapper;


        public Organization_chartServices(IOrganization_chartRepository organization_ChartRepository, ISystem_number_tableServices system_Number_TableServices, IMapper mapper = null, ICapPublisher capPublisher = null) : base(organization_ChartRepository)
        {
            this.organization_ChartRepository = organization_ChartRepository;
            this.system_Number_TableServices = system_Number_TableServices;
            this.mapper = mapper;
            this.capPublisher = capPublisher;
        }


        /// <summary>
        /// 递归显示组织架构
        /// </summary>
        /// <param name="oId">主编号</param>
        /// <returns>递归显示组织架构</returns>
        public List<DepMinrInforDTO> GetOrgainInforShow(string? oName, string? oId)
        {
            if (!string.IsNullOrEmpty(oName))
            {
                oId = organization_ChartRepository.GetShowObj(c => c.department_name == oName && c.department_name == oName).department_number;
                oId = organization_ChartRepository.GetShowObj(c => c.department_number == oId).department_number;
            }
            var list = GetMuInfor(oId);
            return list;
        }


        public List<DepMinrInforDTO> GetMuInfor(string oId)
        {
            var list = organization_ChartRepository.GetShowQueryable(c => c.department_fid == oId);

            var olist = list.ToList().Select(c => new DepMinrInforDTO
            {
                department_number = c.department_number,
                department_name = c.department_name,
                department_institution = c.department_institution,
                department_person = c.department_person,
                department_status = c.department_status,
                department_remark = c.department_remark,
                department_num = c.department_num,
                department_fid = c.department_fid,
                children = GetMuInfor(c.department_number)
            }).ToList();
            return olist.Count() == 0 ? null : olist;
        }


        /// <summary>
        /// 组织架构添加
        /// </summary>
        /// <param name="organiAddDTO">对象</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> PostAddOrgainInfor(OrganiAddDTO organiAddDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            organization_chart chart = new organization_chart();
            chart = mapper.Map<organization_chart>(organiAddDTO);
            chart.department_num = 0;
            var res = await organization_ChartRepository.AddInfor_Async(chart);
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "添加成功";



                //添加成功需要传一个消息队列  传递哪个模块 还有编号
                string numberName = "组织架构-" + organiAddDTO.department_number;
                capPublisher.Publish("GetNewNumber", numberName);

            }
            else
            {
                apiReturn.Code = 500;
                apiReturn.Message = "添加失败";
            }
            return apiReturn;
        }

        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="oId">部门编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> DelPostInfor(string? oId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            //先判断该部门有没有人数
            var res = await organization_ChartRepository.GetShowObjAsync(c => c.department_number == oId);
            if (res == null)
            {
                apiReturn.Code = 400;
                apiReturn.Message = "未找到该部门";
            }
            else
            {
                if (res.department_num > 0)
                {
                    apiReturn.Code = 400;
                    apiReturn.Message = "该部门已有用户，不可删除";
                }
                else
                {
                    //判断是不是最后一级
                    var ges = await organization_ChartRepository.GetShowObjAsync(c => c.department_fid == oId);
                    if (ges != null)
                    {
                        apiReturn.Code = 400;
                        apiReturn.Message = "该部门已有子集，不可删除";
                    }
                    else
                    {
                        var a = await organization_ChartRepository.DelInfor_Async(res);
                        if (a > 0)
                        {
                            apiReturn.Code = 200;
                            apiReturn.Message = "删除成功";
                        }
                        else
                        {
                            apiReturn.Code = 500;
                            apiReturn.Message = "删除失败";
                        }
                    }
                }
            }
            return apiReturn;

        }

        /// <summary>
        /// 反填部门
        /// </summary>
        /// <param name="oId">编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<organization_chart>> FanInforOrm(string? oId)
        {
            ApiReturn<organization_chart> apiReturn = new ApiReturn<organization_chart>();
            var res = await organization_ChartRepository.GetShowObjAsync(c => c.department_number == oId);
            if (res == null)
            {
                apiReturn.Code = 500;
                apiReturn.Message = "请先现在修改部门";
            }
            else
            {
                apiReturn.Code = 200;
                apiReturn.Message = "反填成功";
                apiReturn.ObjInfor = res;
            }
            return apiReturn;
        }


        /// <summary>
        /// 组织架构修改
        /// </summary>
        /// <param name="organiAddDTO">对象</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> PostUpdOrgainInfor(OrganiAddDTO organiAddDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var res = await organization_ChartRepository.GetShowObjAsync(c => c.department_number == organiAddDTO.department_number);
            if (res != null)
            {
                res.department_number = organiAddDTO.department_number;
                res.department_name = organiAddDTO.department_name;
                res.department_status = organiAddDTO.department_status;
                res.department_person = organiAddDTO.department_person;
                res.department_remark = organiAddDTO.department_remark;
                res.department_fid = organiAddDTO.department_fid;
                res.department_institution = organiAddDTO.department_institution;
                var a = await organization_ChartRepository.UpdInfor_Async(res);
                if (a > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "修改成功";
                }
                else
                {
                    apiReturn.Code = 500;
                    apiReturn.Message = "修改失败";
                }
            }
            else
            {
                apiReturn.Code = 500;
                apiReturn.Message = "请选择要修改的编号";
            }
            return apiReturn;
        }
    }
}
